<?php

namespace Modules\Api\Http\Controllers;

use Illuminate\Http\Request;
use Modules\Api\Service\StoreOrderService;

class StoreOrderController extends BaseController
{
    /**
     * 结算台--预生成订单(详情->立即购买)
     *
     * 2019 11 14
     */
    public function pre_build_order(Request $request)
    {
        $arr_input = $request->all();

        $res = StoreOrderService::pre_build_order((array)$arr_input);

        return response_json($res);
    }

    /**
     * 结算台--预生成订单(购物车进入)
     *
     * 2019 11 14
     */
    public function pre_build_order_by_car(Request $request)
    {
        $arr_input = $request->all();

        $res = StoreOrderService::pre_build_order_by_car((array)$arr_input);

        return response_json($res);
    }

    /**
     * 生成订单（订单创建成功后用户端唤起支付）
     *
     * 2019 11 15
     */
    public function create_order(Request $request)
    {
        $arr_input = $request->all();

        $order_data = json_decode($arr_input['order_data'], true);
        $order_data['user_id'] = api_user_id(); //用户id

        shiwu_start();
        $res = StoreOrderService::create_order((array)$order_data);
        shiwu_end($res);

        return response_json($res);
    }

    /**
     * 支付订单
     *
     * 2019 11 18
     */
    public function pay_order(Request $request)
    {
        $n_id = $request->input('order_id');
        $n_uid = api_user_id(); //用户id

         $res = StoreOrderService::check_pay_order_api((int)$n_id, $n_uid);//支付前检查

         $res2 = StoreOrderService::order_result_for_ptshp((int)$n_id);//支付后检查


        return response_json([$res, $res2]);
    }

    /**
     * 取消订单
     *
     * 2019 11 18
     */
    public function cancel_order(Request $request)
    {
        $arr_input = $request->all();
        $arr_input['user_id'] = api_user_id(); //用户id

        shiwu_start();
        $res = StoreOrderService::cancel_order((array)$arr_input);
        shiwu_end($res);

        return response_json($res);
    }

    /**
     * 申请退款-申请退货
     *
     * 2019 11 19
     */
    public function refund_order(Request $request)
    {
        $arr_input = $request->all();
        $arr_input['user_id'] = api_user_id(); //用户id

        shiwu_start();
        $res = StoreOrderService::refund_order((array)$arr_input);
        shiwu_end($res);

        return response_json($res);
    }

    /**
     * 获取用户订单列表
     *
     * 2019 11 20
     */
    public function get_order_list(Request $request)
    {
        $arr_input = $request->all();
        $arr_input['user_id'] = api_user_id(); //用户id

        $res = StoreOrderService::get_order_list((array)$arr_input);

        return response_json($res);
    }


    /**
     * 获取订单详情
     *
     * 2019 11 20
     */
    public function get_order_info(Request $request)
    {
        $arr_input = $request->all();

        $res = StoreOrderService::get_order_info((array)$arr_input);

        return response_json($res);
    }

    /**
     * 删除订单
     *
     * 2019 11 20
     */
    public function del_order(Request $request)
    {
        $arr_input = $request->all();
        $arr_input['user_id'] = api_user_id(); //用户id

        $res = StoreOrderService::del_order((array)$arr_input);

        return response_json($res);
    }

    /**
     * 确认收货
     */
    public function confirm_receipt(Request $request)
    {
        $arr_input = $request->all();
        $arr_input['user_id'] = api_user_id(); //用户id

        $res = StoreOrderService::confirm_receipt((array)$arr_input);

        return response_json($res);
    }

    /**
     * 获取售后订单列表
     */
    public function get_sale_order(Request $request)
    {
        $arr_input = $request->all();
        $arr_input['user_id'] = api_user_id(); //用户id

        $res = StoreOrderService::get_sale_order((array)$arr_input);

        return response_json($res);
    }

    /**
     * 售后订单详情
     */
    public function get_sale_order_info(Request $request)
    {
        $arr_input = $request->all();
        $arr_input['user_id'] = api_user_id(); //用户id

        $res = StoreOrderService::get_sale_order_info((array)$arr_input);

        return response_json($res);
    }

    /**
     * 创建退货物流信息
     */
    public function refund_order_express(Request $request)
    {
        $arr_input = $request->all();

        shiwu_start();
        $res = StoreOrderService::refund_order_express((array)$arr_input);
        shiwu_end($res);

        return response_json($res);
    }
}